﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Diagnostics.Contracts;
using HeapSpecSupport;
using SimpleIR;

namespace Evaluator.EvalState
{
	/// <summary>
	/// This class is used to represent user defined class values (just maps of fields to types).
	/// </summary>
	internal sealed class EUserDefObject : EObject
	{
		/// <summary>
		/// This constructor takes ownership of the <c>fvalues</c> array.
		/// </summary>
		private EUserDefObject(int address, IRClassType tt, AllocRecord alloc, BAPLayoutMap fvalues)
			: base(address, tt, alloc, fvalues)
		{ ;}

		/// <summary>
		/// Allocate and set all the fields to the default values for a struct value.
		/// </summary>
		internal static EUserDefObject CreateFreshObjectValueForType(int address, IRClassType tt, ulong time, long line)
		{
			Contract.Requires(tt != null);
			Contract.Ensures(Contract.Result<EUserDefObject>() != null);

			BAPLayoutMap fve = new BAPLayoutMap(tt.TypeDescription.AllTerminalAccessPaths, time, line);
			AllocRecord alloc = new AllocRecord(time, line);
			return new EUserDefObject(address, tt, alloc, fve);
		}

		public override string ToString()
		{
			StringBuilder sb = new StringBuilder("{" + this.m_loctype.ToString() + ": ");
			sb.Append(this.m_fieldinfo.ToString());
			sb.Append("}");

			return sb.ToString();
		}
	}
}
